package blobDetector;

import java.util.Vector;

public class Blob {
	int minX;
	int minY;
	int maxX;
	int maxY;
	Vector<BlobCell>borders;
	
	
	
	public  float getArea(){
		float area = 0;
		int j;
		for (int i = 0; i <borders.size(); i++) {
			j = i + 1;
			if (j == borders.size())
				j = 0;
			BlobCell c1=borders.get(i);
			BlobCell c2=borders.get(j);
			float x1=c1.x;float y1=c1.y;
			float x2=c2.x;float y2=c2.y;
			area += (x1 + x2) * (y2 - y1);
		}
		return area / 2f;
	}
	
	public Blob(){
		borders=new Vector<BlobCell>();
		minX=Integer.MAX_VALUE;
		maxX=-10000000;
		minY=Integer.MAX_VALUE;
		maxY=-10000000;
	}
	public boolean contains(int x,int y){
		int n=0;
		for (int i=0;i<borders.size();i++){
			BlobCell bCell=borders.get(i);
			if (bCell.x==x&&bCell.y>y){
				n++;
			}
		}
		if (n%2==1)return true;
		else return false;
	}
	
	public boolean[][] containsMatrix(){
		boolean[][] inside=new boolean[maxX-minX+1][maxY-minY+1];
		boolean[][] border=new boolean[maxX-minX+1][maxY-minY+1];
		for (int i=0;i<borders.size();i++){
			BlobCell bCell=borders.get(i);
			border[bCell.x-minX][bCell.y-minY]=true;
			inside[bCell.x-minX][bCell.y-minY]=true;
			border[bCell.x-minX][bCell.y-minY]=true;
		}
		for (int x=0;x<inside.length;x++){
			boolean flag=false;
			for (int y=0;y<inside[x].length-1;y++){
				if (y==0&&border[x][y]){
					flag=true;
				}
				if (flag||border[x][y]){
					inside[x][y]=true;
				}
				if (border[x][y]==false&&border[x][y+1]==true){
					flag=!flag;
				}
			}
			
		}
		return inside;
	}
	

	public int getMinX() {
		return minX;
	}

	public void setMinX(int minX) {
		this.minX = minX;
	}

	public int getMinY() {
		return minY;
	}

	public void setMinY(int minY) {
		this.minY = minY;
	}

	public int getMaxX() {
		return maxX;
	}

	public void setMaxX(int maxX) {
		this.maxX = maxX;
	}

	public int getMaxY() {
		return maxY;
	}

	public void setMaxY(int maxY) {
		this.maxY = maxY;
	}

	public Vector<BlobCell> getBorders() {
		return borders;
	}
	
	public int getWidth(){
		return maxX-minX;
	}
	public int getHeight(){
		return maxY-minY;
	}

	public void setBorders(Vector<BlobCell> borders) {
		this.borders = borders;
	}
	
	public void add(int x,int y){
		borders.add(new BlobCell(x,y));
		minX=Math.min(minX, x);
		minY=Math.min(minY, y);
		maxX=Math.max(maxX, x);
		maxY=Math.max(maxY, y);
	}

}
